package com.sgcc.pms.api.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.sgcc.pms.api.domain.MxpmsCommGblcfg;

import com.sgcc.pms.api.repository.MxpmsCommGblcfgRepository;
import com.sgcc.pms.api.web.rest.util.HeaderUtil;
import com.sgcc.pms.api.web.rest.util.PaginationUtil;
import com.sgcc.pms.api.service.dto.MxpmsCommGblcfgDTO;
import com.sgcc.pms.api.service.mapper.MxpmsCommGblcfgMapper;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing MxpmsCommGblcfg.
 */
@RestController
@RequestMapping("/api")
public class MxpmsCommGblcfgResource {

    private final Logger log = LoggerFactory.getLogger(MxpmsCommGblcfgResource.class);

    private static final String ENTITY_NAME = "mxpmsCommGblcfg";

    private final MxpmsCommGblcfgRepository mxpmsCommGblcfgRepository;

    private final MxpmsCommGblcfgMapper mxpmsCommGblcfgMapper;

    public MxpmsCommGblcfgResource(MxpmsCommGblcfgRepository mxpmsCommGblcfgRepository, MxpmsCommGblcfgMapper mxpmsCommGblcfgMapper) {
        this.mxpmsCommGblcfgRepository = mxpmsCommGblcfgRepository;
        this.mxpmsCommGblcfgMapper = mxpmsCommGblcfgMapper;
    }

    /**
     * POST  /mxpms-comm-gblcfgs : Create a new mxpmsCommGblcfg.
     *
     * @param mxpmsCommGblcfgDTO the mxpmsCommGblcfgDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new mxpmsCommGblcfgDTO, or with status 400 (Bad Request) if the mxpmsCommGblcfg has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/mxpms-comm-gblcfgs")
    @Timed
    public ResponseEntity<MxpmsCommGblcfgDTO> createMxpmsCommGblcfg(@RequestBody MxpmsCommGblcfgDTO mxpmsCommGblcfgDTO) throws URISyntaxException {
        log.debug("REST request to save MxpmsCommGblcfg : {}", mxpmsCommGblcfgDTO);
        if (mxpmsCommGblcfgDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new mxpmsCommGblcfg cannot already have an ID")).body(null);
        }
        MxpmsCommGblcfg mxpmsCommGblcfg = mxpmsCommGblcfgMapper.toEntity(mxpmsCommGblcfgDTO);
        mxpmsCommGblcfg = mxpmsCommGblcfgRepository.save(mxpmsCommGblcfg);
        MxpmsCommGblcfgDTO result = mxpmsCommGblcfgMapper.toDto(mxpmsCommGblcfg);
        return ResponseEntity.created(new URI("/api/mxpms-comm-gblcfgs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /mxpms-comm-gblcfgs : Updates an existing mxpmsCommGblcfg.
     *
     * @param mxpmsCommGblcfgDTO the mxpmsCommGblcfgDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated mxpmsCommGblcfgDTO,
     * or with status 400 (Bad Request) if the mxpmsCommGblcfgDTO is not valid,
     * or with status 500 (Internal Server Error) if the mxpmsCommGblcfgDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/mxpms-comm-gblcfgs")
    @Timed
    public ResponseEntity<MxpmsCommGblcfgDTO> updateMxpmsCommGblcfg(@RequestBody MxpmsCommGblcfgDTO mxpmsCommGblcfgDTO) throws URISyntaxException {
        log.debug("REST request to update MxpmsCommGblcfg : {}", mxpmsCommGblcfgDTO);
        if (mxpmsCommGblcfgDTO.getId() == null) {
            return createMxpmsCommGblcfg(mxpmsCommGblcfgDTO);
        }
        MxpmsCommGblcfg mxpmsCommGblcfg = mxpmsCommGblcfgMapper.toEntity(mxpmsCommGblcfgDTO);
        mxpmsCommGblcfg = mxpmsCommGblcfgRepository.save(mxpmsCommGblcfg);
        MxpmsCommGblcfgDTO result = mxpmsCommGblcfgMapper.toDto(mxpmsCommGblcfg);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, mxpmsCommGblcfgDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /mxpms-comm-gblcfgs : get all the mxpmsCommGblcfgs.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of mxpmsCommGblcfgs in body
     */
    @GetMapping("/mxpms-comm-gblcfgs")
    @Timed
    public ResponseEntity<List<MxpmsCommGblcfgDTO>> getAllMxpmsCommGblcfgs(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of MxpmsCommGblcfgs");
        Page<MxpmsCommGblcfg> page = mxpmsCommGblcfgRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/mxpms-comm-gblcfgs");
        return new ResponseEntity<>(mxpmsCommGblcfgMapper.toDto(page.getContent()), headers, HttpStatus.OK);
    }

    /**
     * GET  /mxpms-comm-gblcfgs/:id : get the "id" mxpmsCommGblcfg.
     *
     * @param id the id of the mxpmsCommGblcfgDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the mxpmsCommGblcfgDTO, or with status 404 (Not Found)
     */
    @GetMapping("/mxpms-comm-gblcfgs/{id}")
    @Timed
    public ResponseEntity<MxpmsCommGblcfgDTO> getMxpmsCommGblcfg(@PathVariable Long id) {
        log.debug("REST request to get MxpmsCommGblcfg : {}", id);
        MxpmsCommGblcfg mxpmsCommGblcfg = mxpmsCommGblcfgRepository.findOne(id);
        MxpmsCommGblcfgDTO mxpmsCommGblcfgDTO = mxpmsCommGblcfgMapper.toDto(mxpmsCommGblcfg);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(mxpmsCommGblcfgDTO));
    }

    /**
     * DELETE  /mxpms-comm-gblcfgs/:id : delete the "id" mxpmsCommGblcfg.
     *
     * @param id the id of the mxpmsCommGblcfgDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/mxpms-comm-gblcfgs/{id}")
    @Timed
    public ResponseEntity<Void> deleteMxpmsCommGblcfg(@PathVariable Long id) {
        log.debug("REST request to delete MxpmsCommGblcfg : {}", id);
        mxpmsCommGblcfgRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
